<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/13
 * Time: 9:55
 */

namespace app\admin\controller;


use think\Controller;
use think\facade\Session;

class Base extends Controller
{

    public function initialize()
    {
        if(!Session::get('admin_id')||Session::get('admin_id')<1){
            $this->redirect(url("login/login"));
        }
        $auth = new Auth();
        $controller = $this->request->controller();
        $action = $this->request->action();
        $name = $controller.'/'.$action;
        $notCheck=[
            'Admin/admindata',
            'Admin/groupdata',
            'Admin/ruledata'
        ];
//        if(!in_array($name, $notCheck)){
//            if (!$auth->check($name,2)){
//                $this->error('你没有权限');
//            }
//        }
    }


    //权限数组处理
    public function roleArray($data){
        $results = [];
        foreach ($data as $key => $value) {
            $results[$value['id']] = $value;
        }
        $tree = [];
        foreach ($results as $item) {
            if(isset($results[$item['pid']])){
                $results[$item['pid']]['subset'][] = &$results[$item['id']];
            }else{
                $tree[] = &$results[$item['id']];
            }
        }
        $results = [];
        foreach ($tree as $value) {
            $value['subset'] = isset($value['subset'])?$this->formatMenus($value['subset']):[];
            $results[] = $value;
        }
        return $results;
    }
    private function formatMenus($items,&$res = array()){
        foreach($items as $item){
            if(!isset($item['subset'])){
                $res[] = $item;
            }else{
                $tem = $item['subset'];
                unset($item['subset']);
                $res[] = $item;
                $this->formatMenus($tem,$res);
            }
        }
        return $res;
    }


    public function upload($file){
        $info = $file->move( '../public/uploads');
        if ($info) {
            $result = [
                'code'     => 1,
                'msg'      => '上传成功',
                'filename' =>'/uploads/' . str_replace('\\', '/', $info->getSaveName())
            ];
            return $result['filename'];
        } else {
            return false;
        }
    }

}